package com.polaris.live.ui.party.fragment.roomInfo

import android.os.Bundle
import androidx.recyclerview.widget.ConcatAdapter
import androidx.recyclerview.widget.LinearLayoutManager
import com.blankj.utilcode.util.StringUtils
import com.polaris.live.adapter.party.RoomInfoMemberAdapter
import com.polaris.live.adapter.party.RoomInfoMemberHeaderAdapter
import com.polaris.live.common.mvvm.base2.BaseVbFragment
import com.polaris.live.common.network.parseState
import com.polaris.live.common.util.ToastGravity
import com.polaris.live.databinding.FragmentMemberBinding
import com.polaris.live.popup.party.RemoveMemberPopup
import com.polaris.live.translation.R
import com.polaris.live.viewmodel.live.RoomInfoMemberViewModel

/**
 * MemberFragment成员
 *
 * @author Created by 天晴 on 2024/10/16 14:42
 * @since 1.0.0
 **/
class MemberFragment : BaseVbFragment<RoomInfoMemberViewModel, FragmentMemberBinding>() {

    private val mRoomId by lazy {
        arguments?.getString("roomId") ?: ""
    }
    private val anchor by lazy {
        arguments?.getBoolean("anchor", false) ?: false
    }
    private val mMemberHeaderAdapter1: RoomInfoMemberHeaderAdapter by lazy {
        RoomInfoMemberHeaderAdapter()
    }
    private val mMemberHeaderAdapter2: RoomInfoMemberHeaderAdapter by lazy {
        RoomInfoMemberHeaderAdapter()
    }
    private val mMemberHeaderAdapter3: RoomInfoMemberHeaderAdapter by lazy {
        RoomInfoMemberHeaderAdapter()
    }
    private val mMemberAdapter1: RoomInfoMemberAdapter by lazy {
        RoomInfoMemberAdapter().apply {
            setRole(RoomInfoMemberAdapter.owner)
        }
    }

    private val mMemberAdapter2: RoomInfoMemberAdapter by lazy {
        RoomInfoMemberAdapter().apply {
            setRole(RoomInfoMemberAdapter.administrator)
        }
    }
    private val mMemberAdapter3: RoomInfoMemberAdapter by lazy {
        RoomInfoMemberAdapter().apply {
            setRole(RoomInfoMemberAdapter.member)
        }
    }
    private val mConcatAdapter = ConcatAdapter(
        mMemberHeaderAdapter1,
        mMemberAdapter1,
        mMemberHeaderAdapter2,
        mMemberAdapter2,
        mMemberHeaderAdapter3,
        mMemberAdapter3
    )

    private var onUserClick: ((userId: Long) -> Unit)? = null

    override fun initView(savedInstanceState: Bundle?) {
        mBinding.recyclerMember.apply {
            layoutManager = LinearLayoutManager(context, LinearLayoutManager.VERTICAL, false)
            itemAnimator = null
            adapter = mConcatAdapter
        }

        initObserve()
        initListener()
        mViewModel.membersSummary(mRoomId)
        mViewModel.membersPage(mRoomId)
        mMemberAdapter3.setAnchor(anchor)

    }

    private fun initListener() {
        mBinding.refreshView.setOnLoadMoreListener {
            ++mViewModel.pageNumber
            mViewModel.membersPage(mRoomId)
        }
        mBinding.refreshView.setOnRefreshListener {
            mViewModel.pageNumber = 0
            mViewModel.membersPage(mRoomId)
        }
        mMemberAdapter1.setOnItemChildClickListener { _, view, position ->
            when (view.id) {
                com.polaris.live.R.id.av_fv -> {
                    onUserClick?.let { click ->
                        mMemberAdapter1.getItemOrNull(position)?.let {
                            click.invoke(it.userId)
                        }
                    }
                }
            }
        }
        mMemberAdapter2.setOnItemChildClickListener { _, view, position ->
            when (view.id) {
                com.polaris.live.R.id.av_fv -> {
                    onUserClick?.let { click ->
                        mMemberAdapter2.getItemOrNull(position)?.let {
                            click.invoke(it.userId)
                        }
                    }
                }
            }
        }
        mMemberAdapter3.setOnItemChildClickListener { _, view, position ->
            val item = mMemberAdapter3.getItemOrNull(position) ?: return@setOnItemChildClickListener
            when (view.id) {
                com.polaris.live.R.id.remove_member -> {
                    RemoveMemberPopup.show(requireContext(), item) {
                        mViewModel.kickMembers(mRoomId, item)
                    }
                }

                com.polaris.live.R.id.av_fv -> {
                    onUserClick?.let { click ->
                        mMemberAdapter3.getItemOrNull(position)?.let {
                            click.invoke(it.userId)
                        }
                    }
                }
            }
        }
    }

    private fun initObserve() {
        mViewModel.kickMembersLiveData.observe(this) { resultState ->
            parseState(resultState, {
                mMemberAdapter3.remove(it)
            }, {
                ToastGravity.showGravity(it.errorMsg)
            })
        }

        mViewModel.membersSummaryLiveData.observe(this) { resultState ->
            parseState(resultState, {
                mMemberAdapter1.setList(mutableListOf(it.anchor))
                mMemberHeaderAdapter1.setList(listOf(1))
                if (it.administrator.isNotEmpty()) {
                    mMemberHeaderAdapter2.setCount(it.administrator.size)
                    mMemberHeaderAdapter2.setList(listOf(2))
                    mMemberAdapter2.setList(it.administrator)
                }
                if (it.currentMemberCount > 0) {
                    mMemberHeaderAdapter3.setCount(it.currentMemberCount.toInt())
                    mMemberHeaderAdapter3.setList(listOf(3))
                }
                mBinding.memberCount.text =
                    "${StringUtils.getString(R.string.member)}: ${it.currentMemberCount}/${it.maxMemberCount}"
            })
        }
        mViewModel.membersPageLiveData.observe(this) { resultState ->
            parseState(resultState, {
                if (mViewModel.pageNumber == 0L) {
                    mMemberAdapter3.data.clear()
                    mMemberAdapter3.setList(it.list)
                } else {
                    mMemberAdapter3.addData(it.list)
                }
                if (mViewModel.pageNumber == 0L) {
                    mBinding.refreshView.finishRefresh()
                } else {
                    mBinding.refreshView.finishLoadMore(0)
                }
                if (it.hasMore) {
                    mBinding.refreshView.setEnableLoadMore(true)
                } else {
                    mBinding.refreshView.setEnableLoadMore(false)
                }
            }, {
                if (mViewModel.pageNumber == 0L) {
                    mBinding.refreshView.finishRefresh(false)
                    mBinding.refreshView.setEnableLoadMore(false)
                } else {
                    mBinding.refreshView.finishLoadMore(0, false, false)
                }
                if (mMemberAdapter3.data.isNotEmpty()) {
                    ToastGravity.showGravity(it.errorMsg)
                }

            })
        }
    }

    fun setListener(onUserClick: (userId: Long) -> Unit) {
        this.onUserClick = onUserClick

    }

}